﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxDataView;

namespace TheheVn
{
    public partial class eWhatWeHave : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                logoText.InnerHtml = "<b>WHAT WE HAVE?</b>";
                Session["TemplateData"] = null;
                LoadTemplateData();
            }
            else
            {
                ASPxDataView1.DataSource = Session["TemplateData"];
                ASPxDataView1.DataBind();
            }
            if (Request.QueryString["d"] != null)
            {
                ShowPopup();
            }
        }

        protected void LoadTemplateData()
        {
            using (var ent = new TheheVn.Entities())
            {
                var data = ent.HtmlDatas.Where(p => p.Type == "WhatWeHave.aspx").OrderBy(p=>p.Priority).ThenByDescending(p=>p.CreateDate);
                var dt=new DataTable();
                dt.Columns.Add("DetailUrl");
                dt.Columns.Add("Image");
                dt.Columns.Add("HtmlText");
                foreach (var d in data)
                {
                    var dr = dt.NewRow();
                    dr["Image"] = d.Image;
                    string str = d.EnHeader??"Untitle";
                    dr["HtmlText"] = str.Length>150?str.Substring(0,149)+"...":str;
                    dr["DetailUrl"] = "eWhatWeHave.aspx?d="+d.Id;
                    dt.Rows.Add(dr);
                }
                Session["TemplateData"] = dt;
                ASPxDataView1.DataSource = dt;
                ASPxDataView1.DataBind();
            }
        }

        protected void ShowPopup()
        {
            if (Request.QueryString["d"] == null) return;
            using (var ent = new TheheVn.Entities())
            {
                try
                {
                    var d = Convert.ToInt32(Request.QueryString["d"]);
                    HtmlData data = ent.HtmlDatas.FirstOrDefault(p => p.Type == "WhatWeHave.aspx" && p.Id == d);
                    if (data == null) return;
                    
                    inner_html.InnerHtml = data.EnHtmlText;
                }
                catch (Exception)
                {
                }
            }
            ASPxPopupControl1.ShowOnPageLoad = true;
        }

        protected void ASPxDataView1_Init(object sender, EventArgs e)
        {
        }
    }
}